<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">

      <el-form-item label="公示题目" prop="publicizeTitle">
        <el-input
          v-model="queryParams.publicizeTitle"
          placeholder="请输入公示题目"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公示人姓名" prop="publicizeName">
        <el-input
          v-model="queryParams.publicizeName"
          placeholder="请输入公示人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公示人工号" prop="publicizeNumber">
        <el-input
          v-model="queryParams.publicizeNumber"
          placeholder="请输入公示人工号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="成果名称" prop="resultName">
        <el-input
          v-model="queryParams.resultName"
          placeholder="请输入成果名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <div style="text-align: right;">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['project:contract:export']">导出</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </div>
      <br>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['project:contract:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['project:contract:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['project:contract:remove']"
        >删除</el-button>
      </el-col>
     <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['project:contract:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contractList" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号"  width="55" :index="indexMethod" align="center"/>

     <!-- <el-table-column label="公示内容" align="center" prop="publicizeContent" />-->
      <el-table-column label="公示题目" align="center" prop="publicizeTitle" />
      <el-table-column label="公示人姓名" align="center" prop="publicizeName" />
      <el-table-column label="公示人工号" align="center" prop="publicizeNumber" />
      <el-table-column label="公示时间" align="center" prop="publicizeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.publicizeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="成果id" align="center" prop="resultId" />
      <el-table-column label="成果名称" align="center" prop="resultName" />
      <el-table-column label="完成单位" align="center" prop="completionUnit" />
      <el-table-column label="负责人姓名" align="center" prop="firstName" />
      <el-table-column label="公示状态" align="center" prop="note10" >
        <template slot-scope="scope">
          <span v-if="scope.row.note10 == 1">已公示</span>
          <span v-if="scope.row.note10 == 2">未公示</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="scope.row.note10=='2'"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['project:gscontract:edit']"
          >提交公示</el-button>
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.note10=='1'"
            icon="el-icon-edit"
            @click="handleUpdate1(scope.row)"
            v-hasPermi="['project:gscontract:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['project:gscontract:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改合同信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="95px">

        <el-container>
         <el-header>
             <el-divider content-position="left">
               <p style="color: #00afff; font-size: 20px;">公示基本信息
               </p>
             </el-divider>
           </el-header>


          <el-main>
            <el-row>
              <el-col span="12" >

                <el-form-item label="公示题目" prop="publicizeTitle">
                  <el-input v-model="form.publicizeTitle" placeholder="请输入公示题目" />
                </el-form-item>
                <!-- <el-form-item label="公示人姓名" prop="publicizeName">
                   <el-input v-model="form.publicizeName" placeholder="请输入公示人姓名" />
                 </el-form-item>-->
              </el-col>

              <!-- <el-form-item label="公示人工号" prop="publicizeNumber">
                 <el-input v-model="form.publicizeNumber" placeholder="请输入公示人工号" />
               </el-form-item>-->
              <el-col span="12" >
              <el-form-item label="公示时间" prop="publicizeTime">
                <el-date-picker clearable
                                v-model="form.publicizeTime"
                                type="date"
                                value-format="yyyy-MM-dd"
                                style="float:left;width: 365.01px"
                                placeholder="请选择公示时间">
                </el-date-picker>
              </el-form-item>
              </el-col>
              <!--<el-col>
                <el-form-item label="公示内容"  prop="publicizeContent">
                  <editor v-model="form.publicizeContent" :min-height="192"/>
                </el-form-item>
              </el-col>-->

              <el-col span="12" >
                <el-form-item label="公示状态"  prop="note10">
                  <el-select v-model="form.note10"  style="width: 100%;"   placeholder="请选择公示状态" >
                    <el-option
                      v-for="item in note10"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </el-main>
        </el-container>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改合同信息对话框 -->
    <el-dialog :title="title1" :visible.sync="open1" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="95px">

        <el-container>
          <el-header>
            <el-divider content-position="left">
              <p style="color: #00afff; font-size: 20px;">公示基本信息
              </p>
            </el-divider>
          </el-header>


          <el-main>
            <el-row>


              <el-col span="12" >

                <el-form-item label="公示题目" prop="publicizeTitle">
                  <el-input v-model="form.publicizeTitle" placeholder="请输入公示题目" />
                </el-form-item>
               <!-- <el-form-item label="公示人姓名" prop="publicizeName">
                  <el-input v-model="form.publicizeName" placeholder="请输入公示人姓名" />
                </el-form-item>-->
              </el-col>

               <!-- <el-form-item label="公示人工号" prop="publicizeNumber">
                  <el-input v-model="form.publicizeNumber" placeholder="请输入公示人工号" />
                </el-form-item>-->
              <el-col span="12" >
                <el-form-item label="公示时间" prop="publicizeTime">
                  <el-date-picker clearable
                                  v-model="form.publicizeTime"
                                  type="date"
                                  value-format="yyyy-MM-dd"
                                  style="float:left;width: 365.01px"
                                  placeholder="请选择公示时间">
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <!--<el-col>
                <el-form-item label="公示内容"  prop="publicizeContent">
                  <editor v-model="form.publicizeContent" :min-height="192"/>
                </el-form-item>
              </el-col>-->

          <el-col span="12" >
                <el-form-item label="公示状态"  prop="note10">
                  <el-select v-model="form.note10"  style="width: 100%;"   placeholder="请选择公示状态" >
                    <el-option
                      v-for="item in note10"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </el-main>
        </el-container>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm1">确 定</el-button>
        <el-button @click="cancel1">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listContract, listContract1,getContract, delContract, addContract, updateContract } from "@/api/module/gs/contract";

export default {
  name: "Contract",
  data() {
    return {
      note10:[{
        value: '1',
        label: '已公示'
      }, {
        value: '2',
        label: '未公示'
      }],

      // 以下websocket
      wsIsRun: false,
      // 定义ws对象
      webSocket: null,
      // ws连接次数
      wsNumber: 0,
      // ws请求链接（类似于ws后台地址）
      ws: '',
      // ws定时器
      wsTimer: null,
      // 以上websocket

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合同信息表格数据
      contractList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      title1: "",
      // 是否显示弹出层
      open1: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        materialsAddress: null,
        applyName1: null,
        applyNumber1: null,
        applyTime1: null,
        auditState1: 2,
        auditTime1: null,
        auditName1: null,
        auditNumber1: null,
        applyName2: null,
        applyNumber2: null,
        applyTime2: null,
        auditState2: null,
        auditTime2: null,
        auditName2: null,
        auditNumber2: null,
        publicizeTime: null,
        publicizeContent: null,
        publicizeTitle: null,
        publicizeName: null,
        publicizeNumber: null,
        resultId: null,
        resultName: null,
        completionUnit: null,
        firstName: null,
        firstNumber: null,
        firstPhone: null,
        firstIdnumber: null,
        enterpriseNumber: null,
        enterpriseName: null,
        corporateName: null,
        corporatePhone: null,
        corporateNumber: null,
        note1: null,
        note2: null,
        note3: null,
        note4: null,
        note5: null,
        note6: null,
        note7: null,
        note8: null,
        note9: null,
        note10: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        note10: [
          { required: true, message: "公示状态不能为空", trigger: "blur" },
        ],
        publicizeTitle: [
            { required: true, message: "公示题目不能为空", trigger: "blur" },
          ],
        publicizeName: [
            { required: true, message: "公示人姓名不能为空", trigger: "blur" },
          ],
        publicizeNumber: [
            { required: true, message: "公示人工号不能为空", trigger: "blur" },
          ],
        publicizeTime: [
          { required: true, message: "公示时间不能为空", trigger: "blur" },
        ],
        publicizeContent: [
          { required: true, message: "公示内容不能为空", trigger: "blur" },
        ],

      }
    };
  },
  created() {
    this.wsIsRun = true
    this.wsNumber = 1
    this.wsInit();
    this.getList();
  },
  methods: {
    //开始
    /**
     * start
     * 初始化ws
     */
    wsInit() {
      // console.log("userName: " + this.$store.state.user.ipp)
      console.log("user: " + this.$store.state.constip.ipp)
      // const wsuri = 'ws://' + '210.47.178.218' + ':8080/library/wsService/' + this.$store.state.user.name
      this.getConfigKey("sys.account.ip").then(response => {
        this.sysipp = response.msg;
      });
      console.log("sysipp: " + this.sysipp)
      const wsuri = 'ws://' + this.sysipp + '/library/wsService/' +
        this.$store.state.constip.ipp + "_" + Math.floor(Math.random() * (99999 - 10000) + 10000);
      console.log("wsuri: " + wsuri)
      this.ws = wsuri
      if (!this.wsIsRun)
        return
      // 销毁ws
      this.wsDestroy()
      // 初始化ws
      this.webSocket = new WebSocket(this.ws)
      // ws连接建立时触发

      this.webSocket.addEventListener('open', this.wsOpenHanler)
      // ws服务端给客户端推送消息

      this.webSocket.addEventListener('message', this.wsMessageHanler)

      // ws通信发生错误时触发
      this.webSocket.addEventListener('error', this.wsErrorHanler)
      // ws关闭时触发

      this.webSocket.addEventListener('close', this.wsCloseHanler)

      clearInterval(this.wsTimer);
    },
    subSend(userId) {

      //获取msg的value
      // var msg = document.getElementById("msg").value;

      this.webSocket.send(userId);

      // ws服务端给客户端推送消息

      // document.getElementById("msg").value="";
    },


    wsOpenHanler(event) {
      console.log('ws建立连接成功')
    },
    wsMessageHanler(e) {

      //Cookies.set('unreadNums', '6')

      this.openMsg(e)
      //const redata = JSON.parse(e.data)
      //console.log(redata)

    },
    /**
     * ws通信发生错误
     */
    wsErrorHanler(event) {
      console.log(event, '通信发生错误')
      console.log("第" + this.wsNumber + "次断线重连(5次未连接将自动断开)")
      this.wsNumber++
      if (this.wsNumber < 6) this.wsInit();
      //this.wsInit()
    },
    /**
     * ws关闭
     */
    wsCloseHanler(event) {
      console.log(event, 'ws关闭')
      console.log("websocket连接超时，已自动断开")
      //this.wsInit()
    },
    /**
     * 销毁ws
     */
    wsDestroy() {
      console.log("hello world")
      if (this.webSocket !== null) {
        this.webSocket.removeEventListener('open', this.wsOpenHanler)
        this.webSocket.removeEventListener('message', this.wsMessageHanler)
        this.webSocket.removeEventListener('error', this.wsErrorHanler)
        this.webSocket.removeEventListener('close', this.wsCloseHanler)
        this.webSocket.close()
        this.webSocket = null
        clearInterval(this.wsTimer)
        console.log("zoul111")
      } else {
        console.log("zoul")
      }
    },
    /**
     * 弹窗
     */
    openMsg(e) {
      const h = this.$createElement;
      if (e.data) {
        console.log(e.data+"222")
        if(e.data==1) {
          this.$router.push({path: "/dashujuzhanshi/dashujushouye"})
        }
        else if(e.data==2){
          this.$router.push({path: "/shou"})
        }else if(e.data==3){
          this.$router.push({path: "/mwnhugongshi"})
        }else if(e.data==4){
          this.$router.push({path: "/liucheng"})
        }else if(e.data==5){
          this.$router.push({path: "/dashujuzhanshi/xueyuan"})
        }else if(e.data==6){
          this.$router.push({path: "/dashujuzhanshi/nianfen"})
        }else if(e.data==10){
          window.scrollBy(0, 100);
        }else if(e.data==11){
          window.scrollBy(0, -100);
        }else if(e.data==21){
          this.$router.push({path: "/ykqlogin3"})
        } else if(e.data==22){
          this.$router.push({path: "/ykqlogin4"})
        }else if(e.data==23){
          this.$router.push({path: "/ykqlogin5"})
        }else if(e.data==24){
          this.$router.push({path: "/shou"})
        }else if(e.data==210){
          this.$router.push({path: "/index"})
        }else if(e.data==211){
          this.$router.push({path: "/transformation"})
        }else if(e.data==212){
          this.$router.push({path: "/wodehetong"})
        }else if(e.data==213){
          this.$router.push({path: "/expense"})
        }else if(e.data==214){
          this.$router.push({path: "/qiye"})
        }else if(e.data==215){
          this.$router.push({path: "/geren"})
        }else if(e.data==216){
          this.$router.push({path: "/chengguogongshi"})
        }else if(e.data==217){
          this.$router.push({path: "/daochuxinxi"})
        }else if(e.data==218){
          this.$router.push({path: "/resultapply"})
        }else if(e.data==12){
          this.queryParams.signingStatus="0";
          console.log(this.queryParams.signingStatus)

        }else if(e.data==13){
          this.queryParams.signingStatus="1";
          console.log(this.queryParams.signingStatus)

        }else if(e.data>500&&e.data<2000){
          let id=e.data-500
          this.$router.push({path: "/detail",query: {id}})

        }else if(e.data>2000&&e.data<3000){
          let nianfen=e.data
          this.$router.push({path: "/dashujuzhanshi/xueyuan",query: {nianfen}})

        }else if(e.data>3000&&e.data<4000){
          let firstcollege=e.data
          let i_xueyuan=firstcollege-3000
          let xueyuan = '';
          if(i_xueyuan<10){
            xueyuan='00'+i_xueyuan;
          }else if(i_xueyuan>=10 && i_xueyuan<100){
            xueyuan='0'+i_xueyuan;
          }else{

          }
          console.log(xueyuan)
          this.$router.push({path: "/dashujuzhanshi/nianfen",query: {xueyuan}})
        }
        else{
          let userId=e.data
          this.$router.push({path: "/view",query: {userId}})

        }
        //结束

        /*var dv = document.getElementById("dv");
        dv.innerHTML += e.data + " ";*/
        /*this.$router.push({path: "/"});*/

        var data = JSON.parse(e.data)
        var route = this.$router;
        var msgType = data.msgType;
        /*if (msgType == "push") {
          // 如果是推送的是一条消息
          this.unreadMsgNums++;
          if (this.unreadMsgNums > 99) {
            this.unreadMsgNumsShow = "99+";
          } else {
            this.unreadMsgNumsShow = String(this.unreadMsgNums);
          }

          this.$notify.info({
            title: data.msgTitle,
            message: data.msgContent,
            onClick() {
              console.log(route)
              route.push({path: "/library/yyg/msg/list"});
            }
          });
        } else if (msgType == "unread") {
          // 如果推送的是未读消息数
          //Cookies.set('unreadNums', data.unreadNums);
          this.unreadMsgNums = Number(data.unreadNums);
          if (this.unreadMsgNums > 0) {
            this.unreadMsgNumsShow = String(this.unreadMsgNums);
            if (this.unreadMsgNums > 99) {
              this.unreadMsgNumsShow = "99+";
            }
            this.$notify.info({
              title: "消息提醒",
              //message: data.msgContent,
              message: "您当前有" + this.unreadMsgNumsShow + "条未读消息，点击此处查看",
              onClick() {
                console.log(route)
                route.push({path: "/library/yyg/msg/list"});
              }
            });
          }
        } else if (msgType == "read") {
          // 如果推送的是用户已读消息
          this.unreadMsgNums--;
          if (this.unreadMsgNums > 0 && this.unreadMsgNums <= 99) {
            this.unreadMsgNumsShow = String(this.unreadMsgNums);
          } else if (this.unreadMsgNums <= 0) {
            this.unreadMsgNumsShow = '';
          }
        }
*/
      }
    },
    //结束


    /** 查询合同信息列表 */
    getList() {
      this.loading = true;
      listContract1(this.queryParams).then(response => {
        this.contractList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    cancel1() {
      this.open1 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        materialsAddress: null,
        applyName1: null,
        applyNumber1: null,
        applyTime1: null,
        auditState1: null,
        auditTime1: null,
        auditName1: null,
        auditNumber1: null,
        applyName2: null,
        applyNumber2: null,
        applyTime2: null,
        auditState2: null,
        auditTime2: null,
        auditName2: null,
        auditNumber2: null,
        publicizeTime: null,
        publicizeContent: null,
        publicizeTitle: null,
        publicizeName: null,
        publicizeNumber: null,
        resultId: null,
        resultName: null,
        completionUnit: null,
        firstName: null,
        firstNumber: null,
        firstPhone: null,
        firstIdnumber: null,
        enterpriseNumber: null,
        enterpriseName: null,
        corporateName: null,
        corporatePhone: null,
        corporateNumber: null,
        note1: null,
        note2: null,
        note3: null,
        note4: null,
        note5: null,
        note6: null,
        note7: null,
        note8: null,
        note9: null,
        note10: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    indexMethod(index) {
      index = (index + 1) + (this.queryParams.pageNum - 1) * this.queryParams.pageSize
      return index
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加合同信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getContract(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "提价公示信息";
      });
    },
    /** 修改按钮操作 */
    handleUpdate1(row) {
      this.reset();
      const id = row.id || this.ids
      getContract(id).then(response => {
        this.form = response.data;
        this.open1 = true;
        this.title1 = "修改合同信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateContract(this.form).then(response => {
              this.$modal.msgSuccess("提交公示成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContract(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮 */
    submitForm1() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateContract(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open1 = false;
              this.getList();
            });
          } else {
            addContract(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除合同信息编号为"' + ids + '"的数据项？').then(function() {
        return delContract(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('project/contract/export', {
        ...this.queryParams
      }, `contract_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
